package 链表题目;

import 链表题目.model.ListNode;


// 找倒数第k个节点
// https://leetcode.cn/problems/lian-biao-zhong-dao-shu-di-kge-jie-dian-lcof/
class 找倒数第k个节点 {
    public ListNode trainingPlan(ListNode head, int cnt) {
        ListNode slow = head, fast = head;
        for (int i = 0; i < cnt; i++) {
            fast = fast.next;
        }
        while (fast != null) {
            slow = slow.next;
            fast = fast.next;
        }
        return slow;
    }

    public static void main(String[] args) {
        ListNode one5 = new ListNode(5, null);
        ListNode one4 = new ListNode(4, one5);
        ListNode one3 = new ListNode(3, one4);
        ListNode one2 = new ListNode(2, one3);
        ListNode one1 = new ListNode(1, one2);
    }
}
